package com.springboot_blog.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.springboot_blog.dto.RoleDTO;
import com.springboot_blog.entity.Role;
import com.springboot_blog.enums.ErrorCodeEnum;
import com.springboot_blog.exception.CustomException;
import com.springboot_blog.mapper.RoleMapper;
import com.springboot_blog.service.RoleService;
import com.springboot_blog.utils.GeneralConverterUtil;
import com.springboot_blog.vo.PageResult;
import com.springboot_blog.vo.RoleVO;
import jakarta.annotation.Resource;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.List;

/**
 * 角色服务实现类
 */
@Slf4j
@Service
@RequiredArgsConstructor
public class RoleServiceImpl extends ServiceImpl<RoleMapper, Role> implements RoleService {

    @Resource
    private RoleMapper roleMapper;

    @Override
    public PageResult<RoleVO> getRoleList(RoleDTO roleDTO) {
        LambdaQueryWrapper<Role> queryWrapper = new LambdaQueryWrapper<Role>()
                .like(StringUtils.hasText(roleDTO.getRoleName()), Role::getRoleName, roleDTO.getRoleName())
                .like(StringUtils.hasText(roleDTO.getRoleCode()), Role::getRoleCode, roleDTO.getRoleCode())
                .like(StringUtils.hasText(roleDTO.getDescription()), Role::getDescription, roleDTO.getDescription())
                .eq(roleDTO.getStatus() != null, Role::getStatus, roleDTO.getStatus())
                .eq(Role::getDeleted, 0);

        //分页查询
        IPage<Role> page = new Page<>(roleDTO.getCurrent(), roleDTO.getSize());
        IPage<Role> pageResult = this.page(page, queryWrapper);

        List<RoleVO> roleVOS = GeneralConverterUtil.convertList(pageResult.getRecords(), RoleVO.class);

        return new PageResult<>(roleVOS, pageResult.getTotal(),
                pageResult.getCurrent(), pageResult.getSize());

    }

    @Override
    public RoleVO getRoleById(Long id) {
        Role role = this.getById(id);
        if (role == null) {
            throw new CustomException(ErrorCodeEnum.ROLE_NOT_FOUND);
        }
        return GeneralConverterUtil.convert(role, RoleVO.class);
    }

    @Override
    public boolean addRole(RoleDTO roleDTO) {
        // 检查角色编码是否已存在
        LambdaQueryWrapper<Role> queryWrapper = new LambdaQueryWrapper<Role>()
                .eq(Role::getRoleCode, roleDTO.getRoleCode())
                .eq(Role::getDeleted, 0);

        if (this.count(queryWrapper) > 0) {
            throw new CustomException(ErrorCodeEnum.ROLE_CODE_EXISTS);
        }

        Role role = GeneralConverterUtil.convert(roleDTO, Role.class);

        return this.save(role);
    }

    @Override
    public boolean updateRole(Long id, RoleDTO roleDTO) {
        Role role = this.getById(id);

        if (role == null) {
            throw new CustomException(ErrorCodeEnum.ROLE_NOT_FOUND);
        }

        role.setRoleName(roleDTO.getRoleName());
        role.setRoleCode(roleDTO.getRoleCode());
        role.setDescription(roleDTO.getDescription());
        role.setStatus(roleDTO.getStatus());

        return this.updateById(role);
    }

    @Override
    @Transactional
    public boolean deleteRoleById(Long id) {
        Role role = this.getById(id);
        if (role == null) {
            throw new CustomException(ErrorCodeEnum.ROLE_NOT_FOUND);
        }
        return this.removeById(id);
    }

    @Override
    @Transactional
    public boolean batchDeleteRoles(List<Long> ids) {
        if (ids == null || ids.isEmpty()) {
            throw new CustomException(ErrorCodeEnum.PARAM_ERROR);
        }

        ids.forEach(roleId -> {
            Role role = this.getById(roleId);
            if (role == null) {
                throw new CustomException(ErrorCodeEnum.ROLE_NOT_FOUND);
            }
            this.removeById(roleId);
        });

        return true;
    }

    @Override
    public List<RoleVO> getEnabledRoles() {
        LambdaQueryWrapper<Role> queryWrapper = new LambdaQueryWrapper<Role>()
                .eq(Role::getDeleted, 0)
                .eq(Role::getStatus, 1);
        List<Role> roleList = this.list(queryWrapper);
        return GeneralConverterUtil.convertList(roleList, RoleVO.class);
    }

}
